// JavaScript Document


function submitGuess() {
	$.get('sendUpdate.php?gameid=' + GL_GAMEID + '&type=guess&guess=' + $('#g').val());	
	return false;
}
function beginRound() {
	$.get('sendUpdate.php?gameid=' + GL_GAMEID + '&type=startgame', function(data) {
		$('#ma_currentWordArea').html(data);
	});
}

// Assumes 
function updateGameData() {
	$.get('getGameInfo.php?gameid=' + GL_GAMEID, function(data) {
		gameData = data.split(',,,');
		/*
		 roundtime
		 users
		 userturn
		 gamestats
		 imagequery
		 dropid
		 dropsrc
		 go
		 guess
		 */
		 
		$('#ma_roundTimer').html(gameData[0]);
		$('#ma_userArea').html(gameData[1]);
		GL_CURRENT_USER = gameData[2];
		$('#ma_scoreArea').html(gameData[3]);
		GL_QUERY = gameData[4];
		GL_DROPID = gameData[5];
		GL_DROPSRC = gameData[6];
		GL_GO = gameData[7];
		$('#ma_guessArea').html(gameData[8]);
		word = gameData[9];
		if(GL_WORD != word) {
			$('#ma_currentWordArea').fadeOut(200);
			$('#ma_currentWordArea').fadeIn(200);
			$('#ma_currentWordArea').fadeOut(200);
			$('#ma_currentWordArea').fadeIn(200);
			$('#ma_currentWordArea').fadeOut(200);
			$('#ma_currentWordArea').fadeIn(200);
			clearBoard();
			GL_WORD = word;
		}
		GL_MOVEID++;
		if(GL_USERNAME == gameData[2]) {
			// If the round is not active
			if(GL_GO == 0) {
				// Make sure the round button is visible
				$('#ma_beginRoundButton').show();
				$('#ma_clearBoardButton').show();
			} else {
				$('#ma_beginRoundButton').hide();
				$('#ma_currentWordArea').html(word);
			}
		} else {
			$('#ma_clearBoardButton').hide();
			$('#ma_currentWordArea').html('?');
		}

	});
}

function startGame() {
	drag = parseInt($('#sa_maximages').val());
	if(drag <1 || drag>5) {
		alert('Images must be between 1-5');
		return;
	}
	drop = parseInt($('#sa_maxholders').val());
	if(drop<1 || drop>10) {
		alert ('Holders must be between 1-10');
		return;
	}
	
	roundtime = parseInt($('#sa_roundtime').val());
	if(roundtime < 10 || roundtime > 60) {
		alert('Round time must be between 10-60');
		return;
	}
	
	numrounds = parseInt($('$sa_numrounds').val());
	if(numrounds < 0 || roundtime > 100) {
		alert('Number of rounds must be between 1-100');
		return;
	}

	
	gamename = $('#sa_gamename').val();
	
	$('#sa_gameOptions').submit();
}

function joinExistingGame() {
	$('#sa_gameInfo').html('<p><b>Joining Existing Game...</b></p>');
	$('#sa_newgame').css('visibility','visible');
	$('#sa_maximages').css('display','none');
	$('#sa_maxholders').css('display','none');
	$('#sa_roundtime').css('display','none');
	$('#sa_numrounds').css('display','none');
	$('#sa_action').val('join');
}

function startNewGame() {
	$('#sa_action').val('new');
	$('#sa_gameInfo').html('<p><b>Configure New Game:</b></p>');
	$('#sa_newgame').css('visibility','visible');
}


function setUserName() {
	$('#sa_gameInfo').css('visibility','visible');
	username = $('#username').val();
	$('#sa_userInfo_submit').html("<b>Username '" + username + "' accepted!</b>");
	$('#sa_username').val(username);
	return false;
}

function positionSetupArea() {
	left = window.innerWidth/2 - 150;
	top = window.innerHeight/2 - 160;
	$('#sa').css('left',left);
	$('#sa').css('top',top);
}

function resizeContentArea(numImages, imageWidth) {

	width_ma = 300;
	padding_ma = 30;
	width_ga = window.innerWidth - 35 - width_ma -padding_ma;
	
	// Check widths
	
	heightDivideFactor = 3;
	doubleHeight = false;
	j=0;
	top = 60;
	left = 40;
	for(i=0; i<numImages; i++) {
		if((j+1)*imageWidth > width_ga) {
			top += imageWidth - 20;
			j=0;
			doubleHeight = true;
		}
		$('#drag' + i).css('left',j*imageWidth + left);
		$('#drag' + i).css('top',top);
		j++;
	}
	if(doubleHeight) {
		heightDivideFactor *= 2/3;
	}
	
	height_ga_player = window.innerHeight/heightDivideFactor - 40;
	height_ga_group = height_ga_player * 2;
	

	$("#ga_player_board").height(height_ga_player);
	$("#ga_group_board").height(height_ga_group);
	$("#ga").width(width_ga);
	$("#ma").width(width_ma);
	$("#ma").height(height_ga_player+height_ga_group + 70 - padding_ma);
	

}
